package com.phoenix.blacklistmanager;

import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class BlackListMainActivity extends Activity {

	private ListView listView;
	private ArrayList<String> listContact;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_black_list_main);
		// declare listview to hold list contact
        if ((ListView)findViewById(R.id.listViewContact) != null) {
        	listView = (ListView)findViewById(R.id.listViewContact);			
		}
//        declare list array
        listContact = new ArrayList<String>();
//        get contact in phone
        Cursor cursor = getContacts();
        while (cursor.moveToNext()) {
            String displayName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
//            String number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
            if (!listContact.contains(displayName)) {
                listContact.add(displayName);
			}
        }
//        fill contact to listview

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        		  android.R.layout.simple_list_item_1, android.R.id.text1, listContact);
        		// Assign adapter to ListView
        listView.setAdapter(adapter); 

        listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
//				Intent intent = new Intent(BlacklistActivity.this,BlacklistDetail.class);
//				Bundle bundel = new Bundle();
//				bundel.putString("contactname", listContact.get(arg2));
//				intent.putExtras(bundel);
//				startActivity(intent);
			}
        });
//        Log.i("start call services", "services is started");
//        if (  startService(new Intent(BlacklistActivity.this,BlacklistService.class))!=null) {
//			
//		}
//        else
//        {
//        	startService(new Intent(BlacklistActivity.this,BlacklistService.class));
//        }
     }
    
    private Cursor getContacts() {
        // Run query
        Uri uri = Phone.CONTENT_URI;
        String[] projection = new String[] { Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER};
        String selection = Phone.IN_VISIBLE_GROUP + " = '" + ("1") + "'";
        String[] selectionArgs = null;
        String sortOrder = Phone.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
        
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        	 moveTaskToBack(true);
             return true;
        }
        return super.onKeyDown(keyCode, event);
    }


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_black_list_main, menu);
		return true;
	}

}
